// pages/profile/profile.js
import { userData } from './behavior'
import { UploadFile, updateUserInfo } from '@/api/user'
import { setStorage } from '@/utils/storage'
import { toast } from '@/utils/extendApi'
Page({
  // 页面的初始数据
  behaviors: [userData],
  data: {
    isShowPopup: false, // 控制更新用户昵称的弹框显示与否
  },
  async changeimg(e){
    const { avatarUrl } = e.detail
    const res = await UploadFile(avatarUrl,'file')
    this.setData({'userInfo.headimgurl':res.data})
  },
  async save(){
    const { code } = await updateUserInfo(this.data.userInfo)
    // 将最新的用户消息存储到store、本地
    if(code === 200){
      setStorage('userInfo',this.data.userInfo)
      this.setUserInfo(this.data.userInfo)
      toast({title: "用户信息更新成功"})
    }
  },
  submitName(e){
    const { nickname } = e.detail.value
    this.setData({
      isShowPopup: false,
      'userInfo.nickname': nickname
    })
  },
  // 显示修改昵称弹框
  onUpdateNickName() {
    this.setData({
      isShowPopup: true,
      'userInfo.nickname': this.data.userInfo.nickname
    })
  },

  // 弹框取消按钮
  cancelForm() {
    this.setData({
      isShowPopup: false
    })
  }
})
